<?php

namespace app\middleware;

use app\BaseController;
use thans\jwt\facade\JWTAuth;
use think\auth\Auth;


class AuthCheck extends BaseController
{
    public function handle($request, \Closure $next)
    {
        //获取头部信息
        $headerInfo = $request->header();
        //判断token是否存在
        if (empty($headerInfo['authorization']) || !isset($headerInfo['authorization'])) {
            return $this->errorPut('未登录',[],'-202');
        }

        //验证token
        $tokenStatus = JwtAuth::auth();
        if (!is_array($tokenStatus)) {
            return $this->errorPut('请先登录',[],'-202');
        }

        $url = $request->url();//当前路由

        $auth=new Auth();
        $check = $auth->check($url,$tokenStatus['admin_id']->getValue());
        //验证权限
        if (!$check) {
             return $this->errorPut('您暂时没有权限，请联系管理员',[],'-201');
        }


        //todo   其他验证可再添加   (判断用户名密码判断最后一次登录的IP是否一致)
        return $next($request);
    }
}
